Page({
  data: {
    notes: [],
  },
  onLoad: function () {
    this.loadNotes();
  },
  onShow: function () {
    this.loadNotes();
  },
  loadNotes: function () {
    const notes = wx.getStorageSync('notes') || [];
    this.setData({ notes });
  },
  addNote: function () {
    wx.navigateTo({
      url: '/note/add/index',
    });
  },
  viewNote: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/note/edit/index?id=${id}`,
    });
  },
  deleteNote: function(e) {
    const id = e.currentTarget.dataset.id;
    const notes = this.data.notes.filter(note => note.id !== id);
    this.setData({ notes });
    wx.setStorageSync('notes', notes); // 更新本地存储
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    });
  },
});